package program.conditions;

import item.*;
import attribute.*;

/**
 * A class concerning can-hit-robot-conditions: (can-hit-robot).  
 * 
 */
public class CanHitRobot extends Conditions {

	/**
	 * Initialise this new can-hit-robot-condition with a line number.
	 * 
	 */
	public CanHitRobot(int lineNb) {
		super(lineNb);
	}

	/**
	 * Evaluate this condition for a given robot.
	 * 
	 * @return      | ...
	 */
	@Override
	public boolean evaluate(Robot robot) {
		if( ! super.evaluate(robot))	return false;
		if(getFirstRobotPosition(robot) != null) return true;
		return false;
	}

	/**
	 * Returns a textual representation of this can-hit-robot-condition,
	 * formatted in accordance with its position in the program.
	 * 
	 * @return      The result is a pretty formatted string of this can-hit-robot-condition,
	 *                      taking in account his relative position in the program.
	 *                      | result == getIndentation(indentationLevel) + "(can-hit-robot)"
	 */
	@Override
	public String prettyFormat(int indentationLevel) {
		return getIndentation(indentationLevel) + "(can-hit-robot)";
	}

	public Position getFirstRobotPosition(Robot robot){
		switch(robot.getOrientation()){
		case UP:
			for(long y = robot.getPosition().getY() -1; Robot.isValidPosition(robot.getPosition().getX(), y, robot.getPosition().getBoard()); y--){
				Position p = robot.doesPositionAlreadyExist(robot.getPosition().getX(), y, robot.getPosition().getBoard());
				if(p.hasItemOfClass(Robot.class)) return p;
			}
			break;
		case RIGHT:
			for(long x = robot.getPosition().getX() +1; Robot.isValidPosition(x, robot.getPosition().getY(), robot.getPosition().getBoard()); x++){
				Position p = robot.doesPositionAlreadyExist(x, robot.getPosition().getY(), robot.getPosition().getBoard());
				if(p.hasItemOfClass(Robot.class)) return p;
			}
			break;
		case DOWN: 
			for(long y = robot.getPosition().getY() +1; Robot.isValidPosition(robot.getPosition().getX(), y, robot.getPosition().getBoard()); y++){
				Position p = robot.doesPositionAlreadyExist(robot.getPosition().getX(), y, robot.getPosition().getBoard());
				if(p.hasItemOfClass(Robot.class)) return p;
			}
			break;
		case LEFT: 
			for(long x = robot.getPosition().getX() -1; Robot.isValidPosition(x, robot.getPosition().getY(), robot.getPosition().getBoard()); x--){
				Position p = robot.doesPositionAlreadyExist(x, robot.getPosition().getY(), robot.getPosition().getBoard());
				if(p.hasItemOfClass(Robot.class)) return p;
			}
		}
		return null;		
	}
}
